<span id="{{ .URL }}" class="leancloud_visitors meta_data_item" data-flag-title="">
    <span class="post-meta-item-icon">
      <span class="octicon octicon-eye"></span> 
    </span>
    <i class="fa fa-eye"></i>
    <span class="old-visitors-count" style="display: none;"></span>
    <span class="leancloud-visitors-count"></span>
</span>

<!-- View counter -->
<!--访问LeanCloud的js包装脚本-->
<script src="https://cdn1.lncld.net/static/js/av-core-mini-0.6.1.js"></script>
<!--初始化LeanCloud的AV对象-->
<script>
	AV.initialize("{{ .Site.Params.leancloud_app_id }}", "{{ .Site.Params.leancloud_app_key }}");
</script>
<!--实现计数的js包装脚本-->
<script type="text/javascript">
function showTime(Counter) {
    var query = new AV.Query(Counter);
    var entries = [];
    var $visitors = $(".leancloud_visitors");

    $visitors.each(function() {
        entries.push($(this).attr("id").trim());
    });

    query.containedIn('url', entries);
    query.find()
        .done(function(results) {
            var COUNT_CONTAINER_REF = '.leancloud-visitors-count';
            var OLD_COUNT_CONTAINER_REF = '.old-visitors-count';

            // if (results.length === 0) {
            //     $visitors.find(COUNT_CONTAINER_REF).text(0);
            //     return;
            // }

            for (var i = 0; i < results.length; i++) {
                var item = results[i];
                var url = item.get('url');
                var time = item.get('time');
                var element = document.getElementById(url);

                $(element).find(COUNT_CONTAINER_REF).text(time);
            }
            for (var i = 0; i < entries.length; i++) {
                var url = entries[i];
                var element = document.getElementById(url);
                var countSpan = $(element).find(COUNT_CONTAINER_REF);
                if (countSpan.text() == '') {
                    var oldCountSpan = $(element).find(OLD_COUNT_CONTAINER_REF).text();
                    if(oldCountSpan!=''){
                        countSpan.text(0+parseInt(oldCountSpan));
                    }else{
                        countSpan.text(0);          
                    }
                }
            }
        })
        .fail(function(object, error) {
            console.log("Error: " + error.code + " " + error.message);
        });
}

function addCount(Counter) {
    var $visitors = $(".leancloud_visitors");
    var url = $visitors.attr('id').trim();
    var title = $visitors.attr('data-flag-title').trim();
    var query = new AV.Query(Counter);

    query.equalTo("url", url);
    query.find({
        success: function(results) {
            if (results.length > 0) {
                var counter = results[0];
                counter.fetchWhenSave(true);
                counter.increment("time");
                counter.save(null, {
                    success: function(counter) {
                        var $element = $(document.getElementById(url));
                        $element.find('.leancloud-visitors-count').text(counter.get('time'));
                    },
                    error: function(counter, error) {
                        console.log('Failed to save Visitor num, with error message: ' + error.message);
                    }
                });
            } else {
                var newcounter = new Counter();
                /* Set ACL */
                var acl = new AV.ACL();
                acl.setPublicReadAccess(true);
                acl.setPublicWriteAccess(true);
                newcounter.setACL(acl);
                /* End Set ACL */
                newcounter.set("title", title);
                newcounter.set("url", url);
                var OLD_COUNT_CONTAINER_REF = '.old-visitors-count';
                var $element = $(document.getElementById(url));
                var oldCountSpan = $element.find(OLD_COUNT_CONTAINER_REF).text();
                if(oldCountSpan!=''){
                    newcounter.set("time", parseInt(oldCountSpan)+1);
                }else{
 	                    newcounter.set("time",  1);
                }
                newcounter.save(null, {
                    success: function(newcounter) {
                        var $element = $(document.getElementById(url));
                        $element.find('.leancloud-visitors-count').text(newcounter.get('time'));
                    },
                    error: function(newcounter, error) {
                        console.log('Failed to create');
                    }
                });
            }
        },
        error: function(error) {
            console.log('Error:' + error.code + " " + error.message);
        }
    });
}
$(function() {
    var Counter = AV.Object.extend("Counter");
    //如果只有1个.leancloud_visitors，则说明是博客页，需要显示查看数并将查看数+1
    //超过1个，则说明是文章列表，只需要显示查看数
    if ($('.leancloud_visitors').length == 1) {
        addCount(Counter);
    } else {
        showTime(Counter);
    }
});
</script>
